/**
 * 
 */
package cn.sundat.utils;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Hashtable;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

/**QrCode操作类
 * @author XIN
 *
 */
public class QrcodeUtil {
	/**
	 * 生成二维码
	 * 
	 * @param savepath
	 *            保存的路径
	 * @param content
	 *            保存的内容
	 */
	public static void createQrcode(String savepath, String content) {
		// 配置QrCode
		Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		hints.put(EncodeHintType.MARGIN, 0);
		try {
			Path file = Paths.get(savepath);
			BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 140, 140, hints);
			MatrixToImageWriter.writeToPath(bitMatrix, "png", file);
		} catch (WriterException | IOException e) {
			e.printStackTrace();
		}
	}
}
